<?php
require_once 'pages/TMSAdminPage.inc';

/**
 * @author dkrasilnikov@gmail.com
 * @property string $SaveActionTitle
 * @property string $BackActionTitle
 * @property array $Properties
 * @property IItem $ModelItem
 * @property string $ListUrl
 */
class TMSItemPage extends TMSAdminPage {
	protected $modelItem;
	private $_save_title_ = 'Save';
	private $_back_title_ = 'Open list';
	private $_list_page_;
	
	private $_print_;
	
	public function Handle(THttpRequest $request){
		$this->request = $request;
		$this->modelItem = $this->getItem();
		parent::Handle($request);
	}	
	
	protected function getItem(){
		$item = false;
		if ($this->request->id)
			$item = $this->Model->GetItem($this->request->id);
		if (!$item)
			throw new TCoreException(TCoreException::ERR_RESOURCE_NOT_FOUND);
		return $item;
	}
	
	public function __set($nm,$value){
		switch ($nm){
			case 'SaveActionTitle':$this->_save_title_ = $value;break;
			case 'BackActionTitle':$this->_back_title_ = $value;break;
			case 'Print':$this->_print_ = $value;break;
			default:parent::__set($nm,$value);break;
		}
	}
	
	public function __get($nm){
		switch ($nm){
			case 'SaveActionTitle':return $this->_save_title_;break;
			case 'BackActionTitle':return $this->_back_title_;break;
			case 'Properties':{
				$props = $this->ModelItem->Properties();
				if (empty($this->hideProperties))
					return $props;
				$result = array();
				foreach ($props as $p)
					if (!in_array($p->Name(), $this->hideProperties))
						$result[] = $p;
				return $result;	
			}break;
			case 'ModelItem':return $this->modelItem;break;
			case 'Path':{
				if (is_null($this->_list_page_))
					$this->_list_page_ = $this->service->ListPage($this->Model->ClassName());
				if (!$this->_list_page_) return array();
				return array($this->_list_page_->Url()=>$this->_list_page_->Title());
			}break;
			case 'ListUrl':return $this->service->ListUrl($this->Model->ClassName());break;
			case 'PrintUrl':return $this->_print_?$this->service->Url($this->_print_,null,null,array('id'=>$this->modelItem->ItemId())):null;break;
			default:return parent::__get($nm);break;
		}
	}
	
	protected function doSave($data){
		return $this->Model->EditItem($this->modelItem->ItemId(), $data);
	}
	
	public function http_Save(){
		$props = $this->ModelItem->Properties();
		$data = array();
		foreach ($props as $p){
			$nm = $p->Name();
			if (isset($this->request->$nm)){ 
				switch ($p->Type()){
				case TItemPropertyType::PT_SET:
					$data[$nm] = array_keys($this->request->$nm); break;
				case TItemPropertyType::PT_IMAGE:					
				case TItemPropertyType::PT_FILE:{
					if($this->request->$nm instanceof TUploadedFile)
						if($this->request->$nm->UploadError() != UPLOAD_ERR_NO_FILE)
							$data[$nm] = $this->request->$nm->Store();
				} break;
				default:	
					$data[$nm] = $this->request->$nm; break;
				}
			}
		}
		$this->modelItem = $this->doSave($data);
	}

	public function ReloadItem(){
		if ($this->modelItem){
			$this->modelItem = $this->Model->GetItem($this->modelItem->ItemId());
		}
	}
	
	protected function processUrlParameters(array $parameters){
		if ($this->ModelItem)
			$parameters['id'] = $this->ModelItem->ItemId();
		else if ($this->request->Id)
			$parameters['id'] = $this->request->id;
		return $parameters;
	}
	
	protected function listPath(){
		return substr($this->Name(),0,strrpos($this->Name(), '/'));
	}
		
	public function CollectionUrl(IProperty $property){
		return $this->service->Url($this->listPath().'/'.strtolower($property->Name()),null,null,array('container'=>$this->ModelItem->ItemId()));
	}
}